using System;
using System.Collections.Generic;
using System.Collections;

using System.Text;
using BuildingBasicDomain;
using MVCFramework.Models;
using MVCFramework.Presenter.Commands; //move this to presenter

namespace MVCFramework.Presenter.Selections
{
   public class SelectionManager:IEnumerable 
    {
        Presenter _presenter;
        IModel _model;
        List <Selection> _selections;
        Selection currentSelection;

        internal Selection CurrentSelection
        {
            get { return currentSelection; }
            set { currentSelection = value; }
        }
       internal List<Selection> Selections
        {
            get { return _selections; }
            set { _selections = value; }
        }

        public SelectionManager(Presenter presenter, IModel model)
        {
            _presenter = presenter;
            _model = model;
            currentSelection = new Selection();
        }

        public void Add(ISelectable selected)
        {
            CurrentSelection.Add(selected);
        }

       public bool Find(Predicate<ISelectable> match)
       {
           if (match == null)
           {
               throw new Exception("null referance predicate ");
           }
           foreach(ISelectable selection in this .CurrentSelection )
           {
               if (match(selection))
               {
                   return true;
               }
           }
           return false;
       }


        public void Clear()
        {
            foreach (ISelectable selectable in CurrentSelection )
                selectable.IsSelected = false;
            CurrentSelection.Clear();
        }

        public IList<ISelectable> GetSelected()
        {
            return CurrentSelection;// CurrentSelection;
        }

        public void Remove(ISelectable selected)
        {
            CurrentSelection.Remove(selected);
        }

        public void deleteSelection() // move this to presenter IE
        {
            if (CurrentSelection != null )
            {
                Command deleteCommand = new DeleteSelectionCommand(_model, CurrentSelection);
                if (deleteCommand.Execute())
                    _presenter.AddCommand(deleteCommand);
                CurrentSelection.Clear();
            }
        }

       internal void AddSelectionGroup(Selection selection)
        {

        }

        public void InvertSelection()
        {
            foreach (List <IModelData> datalist in _model)
            {
                foreach (IModelData data in datalist)
                {
                    if (data is ISelectable)
                        if (((ISelectable)data).IsSelected == true)
                            ((ISelectable)data).IsSelected = false;
                        else ((ISelectable)data).IsSelected = true;
                }
            }
        }

        public void GetPreviousSelection()
        {

        }

        public string[] GetSelectionDescription()
        {
            return CurrentSelection.getSelectedDescription();
        }

        private Selection ByPlaneSelection()
        {
            return null;
        }

        private Selection ByGroupSelection()
        {
            return null;
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return CurrentSelection.GetEnumerator();
        }

        #endregion
    }
}
